/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se1603.wwm.DAO.impl;

import edu.fjut.se1603.wwm.DAO.BaseDao;
import edu.fjut.se1603.wwm.DAO.PetDao;
import edu.fjut.se1603.wwm.entity.Pet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daydream
 */
public class PetDaoImpl extends BaseDao implements PetDao {

    @Override
    public int add(Pet pet) {
        int r = 0;
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("insert into pet(ID,NAME,TYPENAME,HEALTH,LOVE,BIRTHDAY,OWNER_ID,STORE_ID,PRICE) values(?,?,?,?,?,?,?,?,?)");
            pst.setInt(1, pet.getId());
            pst.setString(2, pet.getName());
            pst.setString(3, pet.getTypename());
            pst.setInt(4, pet.getHealth());
            pst.setInt(5, pet.getLove());
            pst.setDate(6, pet.getBirthday());
            pst.setInt(7, pet.getOwner_id());
            pst.setInt(8, pet.getStore_id());
            pst.setInt(9, pet.getPrice());
            r = pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;

    }

    @Override
    public Pet getById(int id) {
        Pet pet = new Pet();
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("select * from pet where ID=?");
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                pet.setId(id);
                pet.setName(rs.getString("NAME"));
                pet.setTypename(rs.getString("TYPENAME"));
                pet.setHealth(rs.getInt("HEALTH"));
                pet.setLove(rs.getInt("LOVE"));
                pet.setBirthday(rs.getDate("BIRTHDAY"));
                pet.setOwner_id(rs.getInt("OWNER_ID"));
                pet.setStore_id(rs.getInt("STORE_ID"));
                pet.setPrice(rs.getInt("PRICE"));
            }
            con.close();

        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pet;
    }

    @Override
    public List<Pet> getList() {
        List<Pet> pets = new ArrayList<Pet>();
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("select * from pet");
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                Pet pet = new Pet();
                pet.setId(rs.getInt("ID"));
                pet.setName(rs.getString("NAME"));
                pet.setTypename(rs.getString("TYPENAME"));
                pet.setHealth(rs.getInt("HEALTH"));
                pet.setLove(rs.getInt("LOVE"));
                pet.setBirthday(rs.getDate("BIRTHDAY"));
                pet.setPrice(rs.getInt("PRICE"));
                pet.setOwner_id(rs.getInt("OWNER_ID"));
                pet.setStore_id(rs.getInt("STORE_ID"));
                pets.add(pet);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pets;
    }

    @Override
    public int delete(Integer id) {
        int r = 0;
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("delete from pet where ID=" + id);
            r = pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public int change(Pet pet, Integer ownerid, Integer storeid) {
        int r = 0;
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("update pet set OWNER_ID=?,STORE_ID=? where ID=" + pet.getId());
            pst.setInt(1, ownerid);
            pst.setInt(2, storeid);
            r = pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }
}
